
//
//  ScratchCardView.m
//  carcareIOS
//
//  Created by wr on 15/12/21.
//  Copyright © 2015年 baozun. All rights reserved.
//

#import "ScratchCardView.h"
#import "STScratchView.h"

#define LINE_VIEW_HEIGHT (88 * __MAIN_RATIO_375)
#define FOOTER_BUTTON_Y_HEIGHT ((35 + 50) * __MAIN_RATIO_375)

@interface ScratchCardView()
//主题的抽动view
@property (nonatomic , strong) UIView * scratchView;
@property (nonatomic , strong) STScratchView * sTScratchView;
@property (nonatomic , strong) UILabel * earnMoneyLabel;

@end
@implementation ScratchCardView

- (instancetype) initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        CGFloat scratchViewX = 15 * __MAIN_RATIO_375;
        CGFloat cornerButtonW = 29 * __MAIN_RATIO_375;
        CGFloat cornerButtonX = self.width - cornerButtonW;
        __weak __typeof(self) wself = self;
        UIButton * cornerButton = [Factory buttonWithFrame:CGRectMake(cornerButtonX, 0, cornerButtonW, cornerButtonW) withImageName:@"scratchCancelButtonImage" click:^{
            [AlertMgr dismiss:TYPE_VIEW_POPUP];
        } onView:self];
        cornerButton.layer.cornerRadius = cornerButtonW / 2;
        cornerButton.layer.masksToBounds = YES;

        
        [[[UIView viewWithFrame:CGRectMake(cornerButtonX + cornerButtonW / 2, cornerButtonW - 2, 0.5, LINE_VIEW_HEIGHT - cornerButtonW)] resetBackgroundColor:[[UIColor whiteColor] colorWithAlphaComponent:0.6]] setupOnView:self];
        
        UIImage * headImage = [ImageNamed(@"scratchCardHeadImage")imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        CGFloat headImageVY = LINE_VIEW_HEIGHT - 8;
        UIImageView * headImageV = [Factory imageViewWithFrame:CGRectMake(0, headImageVY, self.frame.size.width, headImage.size.height) image:headImage onView:self];
        headImageV.contentMode = UIViewContentModeScaleToFill;
        
        
        CGFloat scratchViewW = self.frame.size.width  - scratchViewX * 2;
        UIView * backClipView = [[[UIView viewWithFrame:CGRectMake(scratchViewX,headImageVY + headImage.size.height / 5 * 2, scratchViewW, headImage.size.height / 5 * 3)] resetBackgroundColor:[UIColor clearColor]] setupOnView:self];
        backClipView.clipsToBounds = YES;
        
        CGFloat scrtchViewH = self.frame.size.height - headImage.size.height / 5 * 2 - headImageVY - FOOTER_BUTTON_Y_HEIGHT;
        self.scratchView = [[[UIView viewWithFrame:CGRectMake(0, -(scrtchViewH - backClipView.height), scratchViewW, scrtchViewH)] resetBackgroundColor:[UIColor clearColor]] setupOnView:backClipView];
        self.scratchView.clipsToBounds = YES;
        
        [Factory imageViewWithFrame:self.scratchView.bounds image:[ImageNamed(@"scratchBackImage") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] onView:self.scratchView].contentMode = UIViewContentModeScaleToFill;
        
        CGFloat labelX = 10 * __MAIN_RATIO_375;
        CGFloat labelW = self.scratchView.width - labelX * 2;
        CGFloat headLabelH = 25;
        CGFloat headLabelY = 50 * __MAIN_RATIO_375;
        
        [Factory labelWithFrame:CGRectMake(labelX, headLabelY, labelW, headLabelH) font:FONT_DEFAULT_Light(25) text:@"恭喜您获得" textColor:COLOR_RGBA(129, 18, 39, 1) onView:self.scratchView textAlignment:NSTextAlignmentCenter].backgroundColor = [UIColor clearColor];
        
        CGFloat earnMoneyLableY = headLabelH + headLabelY ;
        self.earnMoneyLabel = [Factory labelWithFrame:CGRectMake(labelX, earnMoneyLableY + 20 * __MAIN_RATIO_375,labelW, self.scratchView.height - earnMoneyLableY * 2 - 20 * __MAIN_RATIO_375) font:FONT_DEFAULT_Bold(100 * __MAIN_RATIO_375) text:@"" textColor:COLOR_RGBA(210, 0, 43, 1) onView:self.scratchView textAlignment:NSTextAlignmentCenter];
        self.earnMoneyLabel.backgroundColor = [UIColor clearColor];
        
        
        _sTScratchView = [[STScratchView alloc]initWithFrame:self.scratchView.bounds];
        _sTScratchView.didDisappearView = ^()
        {
//            whiteView.hidden = YES;
        };
        [self.scratchView addSubview:_sTScratchView];
        
        [_sTScratchView setSizeBrush:20.0];
        UIImageView * scratchImageV = [Factory imageViewWithFrame:_sTScratchView.bounds image:[ImageNamed(@"scratchPanImage") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ]onView:nil];
        scratchImageV.contentMode = UIViewContentModeScaleToFill;
        [_sTScratchView setHideView:scratchImageV];
        
        UIImage * shadowImage = [ImageNamed(@"scratchShadowImage") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        [Factory imageViewWithFrame:CGRectMake(0, 0, self.scratchView.width, shadowImage.size.height * __MAIN_RATIO_375) image:shadowImage onView:self.scratchView].contentMode = UIViewContentModeScaleToFill;

        [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.2 initialSpringVelocity:2 options:UIViewAnimationOptionCurveEaseOut animations:^{
            

        } completion:nil];

        
        [UIView animateWithDuration:1 animations:^{
            [backClipView setTop:(fabs(wself.screenViewY) + backClipView.top)];
            [wself.scratchView setTop:0];
            [backClipView setHeight:scrtchViewH];
        
        }];
        
        CGFloat footerButtonH = 50 * __MAIN_RATIO_375;
        CGFloat footerButtonY = (FOOTER_BUTTON_Y_HEIGHT - footerButtonH) + backClipView.top + backClipView.height;
        UIButton * footerH5Button = [Factory buttonWithFrame:CGRectMake(10 * __MAIN_RATIO_375 + backClipView.left, footerButtonY, self.scratchView.width - 10 * __MAIN_RATIO_375 * 2, footerButtonH) bgColor:COLOR_RGBA(251, 190, 12, 1) title:@"还要挣点？点我！" textColor:COLOR_RGBA(133,0,29,1) click:^{
            
        } onView:self];
        footerH5Button.layer.cornerRadius = 4;
        footerH5Button.layer.masksToBounds = YES;
    
    }
    return self;
}
- (void)setEarnMoneyStr:(NSString *)earnMoneyStr
{
    NSString * earnMoneyNewStr = [NSString stringWithFormat:@"%@挣点",earnMoneyStr];
    
    NSMutableAttributedString * earnMoneyAttributeStr = [[NSMutableAttributedString alloc] initWithString:earnMoneyNewStr];
    [earnMoneyAttributeStr addAttribute:NSFontAttributeName value:FONT_DEFAULT_Light(15) range:NSMakeRange([earnMoneyNewStr length] - 2, 2)];
    self.earnMoneyLabel.attributedText = earnMoneyAttributeStr;

}
@end
